package com.study.constrollers;

import com.study.common.Message;
import com.study.pojo.User;
import com.study.services.UserService;
import com.study.util.IDGenerator;
import org.aspectj.weaver.ast.Var;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 *
 * Created by bunny on 2017/8/22.
 */

@Controller
@RequestMapping("/account")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "account/login";
    }

    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Message doLogin(
        HttpServletRequest request,
        HttpServletResponse response,
        User user) {
        Message msg = new Message();

       /* List<User> lst = userService.findAll();
        msg.setData(lst);
        user.setId(IDGenerator.getInstance().nextId());*/

        int result1 = userService.login(user);

        if(result1 == 1){
            msg.setCode(true);
            msg.setData("找到用户");
        }
        else {
            msg.setCode(false);
            msg.setData("没找到用户");
        }

        //msg.setData(userService.find(1));

        return msg;
    }

    @RequestMapping(value = "/registration", method = RequestMethod.GET)
    public String regerstion() {
        return "account/registration";
    }

    @ResponseBody
    @RequestMapping(value = "/regerstion", method = RequestMethod.POST)
    public Message doRegerstion(
        HttpServletRequest request,
        HttpServletResponse response,
        User user) {
        Message msg = new Message();

        user.setId(IDGenerator.getInstance().nextId());
        int result = userService.add(user);

        if (result == 1) {
            msg.setCode(true);
        } else msg.setCode(false);

//        msg.setCode(result == 1);

        return msg;
    }

}
